var CN_SITE_IMPORT = function(){
	this.init();
};
CN_SITE_IMPORT.prototype.init = function(){
	var self = this;
	self.expand();

	self.initSomeThing();
	self.bandClick();
	self.bandResize();
}
/**
 * 拓展方法
 */
CN_SITE_IMPORT.prototype.expand = function(){
	var self = this;
	self.initSomeThing = function() {
		
		var lastUrl = window.location.href;
		// 记住我取值
		var userToken = $.cookie("userToken");
	    if (!$("#siteUserExist").data("exist") && userToken) {
	    	jsonAjax('autoLogin.html', 'json', JSON.stringify({userToken:userToken}), function(data) {
				if(data.status == SysConstant.SUCCESS) {
					window.location.href = lastUrl || ctx;
				} else {
				} 
			});
	    } else {  // 用户已登录
	    	var websocket;
	    	var websocketUrl = "ws://" + window.location.host + ctx + "websocket/siteInnerMessage.html";
            if ('WebSocket' in window) {
                websocket = new WebSocket(websocketUrl);
            } else if ('MozWebSocket' in window) {
                websocket = new MozWebSocket(websocketUrl);
            } else {
            	alert("垃圾浏览器，websocket都不支持，给个alert你回去吔屎啦！");
            }
            websocket.onopen = function (evnt) {
            };
            websocket.onmessage = function (evnt) {
                $("#user-name").append("（<font color='red'>"+evnt.data+"</font>）");
                $("#own-new-message").append("（<font color='red'>"+evnt.data+"</font>）");
            };
            websocket.onerror = function (evnt) {
            };
            websocket.onclose = function (evnt) {
            }
	    }
	};
	self.bandClick = function() {
		// 点击登录按钮
		$("#loginBtn").click(function() {
			if(!validInput("#userName", ['accountAndEmail']) || !validInput("#userPwd", ['password'])) {
				return;
			}
			var userName = $("#userName").val();
			var userPwd = $("#userPwd").val();
			var loginCaptcha = $("#captcha").val();
			if(loginCaptcha) {
				var loadingIndex = submitHolder();
				jsonAjax('login.html', 'json', JSON.stringify({userName:userName, userPwd: userPwd, loginCaptcha: loginCaptcha, rememberMe: $("#rememberMe").prop("checked")}), function(data) {
					closeSubmitHolder(loadingIndex);
					if(data.status == SysConstant.SUCCESS) {
						self.checkRememberMe(data.object);
						self.loginSuccess(data);
					} else {
						tipsFailed(data.msg, 1500);
						self.changeCaptcha();
					} 
				});
			} else {
				tipsBlur("#captcha", "请填写验证码", 1000);
			}
		});
		// 点击注册按钮
		$("#registerBtn").click(function() {
			if(!validInput("#userName", ['account']) || !validInput("#userPwd", ['password'])) {
				return;
			}
			var userName = $("#userName").val();
			var userPwd = $("#userPwd").val();
			var registerCaptcha = $("#captcha").val();
			if(registerCaptcha) {
				var loadingIndex = submitHolder();
				jsonAjax('register.html', 'json', JSON.stringify({userName:userName, userPwd: userPwd, registerCaptcha: registerCaptcha}), function(data) {
					closeSubmitHolder(loadingIndex);
					if(data.status == SysConstant.SUCCESS) {
						self.loginSuccess(data);
					} else {
						tipsFailed(data.msg, 1500);
						self.changeCaptcha();
					} 
				});
			} else {
				tipsBlur("#captcha", "请填写验证码", 1000);
			}
		});
		// 点击登出按钮
		$("#logoutBtn").click(function() {
			 $.cookie("userToken", '', { expires: -1 });
			 window.location.href = ctx + "logout.html";
		});
	};
	self.loginSuccess = function(data) {
		tipsSuccess(data.msg, 1500);
		window.location.href = ctx;
	};
	self.bandResize = function() {
	};
	self.checkRememberMe = function(userToken) {
		$.cookie("userToken", userToken, { expires: 7 }); // 记住我勾选
	};
};
// 供外部调用的方法
CN_SITE_IMPORT.prototype.changeCaptcha = function() {
	$("#captchaImg").attr("src", ctx + "captcha/get/" + Math.random() + ".html");
};
